package com.zjk.storage.core.aspect.download;

import com.zjk.storage.core.aspect.FileStorageAspect;
import com.zjk.storage.core.entity.BaseFileInfo;
import com.zjk.storage.core.provider.FileStorage;
import lombok.Getter;
import lombok.Setter;

import java.io.InputStream;
import java.util.Iterator;
import java.util.function.Consumer;

@Getter
@Setter
public class DownloadAspectChain {

    private DownloadAspectChainCallback callback;
    private Iterator<FileStorageAspect> aspectIterator;
    public DownloadAspectChain(Iterable<FileStorageAspect> aspects, DownloadAspectChainCallback callback) {
        this.aspectIterator = aspects.iterator();
        this.callback = callback;
    }
    public void next(BaseFileInfo fileInfo, FileStorage fileStorage, Consumer<InputStream> consumer) {
        if (aspectIterator.hasNext()) {
            aspectIterator.next().downloadAround(fileInfo, fileStorage,consumer, this);
        } else {
            callback.run(fileInfo, fileStorage,consumer);
        }
    }
}
